package cn.gzy.mapper;

import cn.gzy.pojo.User;
import cn.gzy.vo.Follow;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {
    User searchByUsername(@Param("username") String username);
    User searchByEmail(@Param("email") String email);
    Integer updatePasswordByCondition(@Param("user") User user, @Param("condition") User condition, @Param("columnName") String columnName);


    @Select("SELECT * FROM users WHERE account = #{account} AND password = #{password}")
    User searchByAccountAndPassword(@Param("account") String account, @Param("password") String password);

    Integer updateByCondition(@Param("user") User user,@Param("condition") User condition);

    Integer countOfFollow(@Param("id")Integer id,
                          @Param("type") Integer type,
                          @Param("key")String key);
    List<Follow> followship(@Param("id")Integer id,
                            @Param("type") Integer type,
                            @Param("current")Integer current,
                            @Param("size")Integer size,@Param("key")String key);
    @Select("select userId,account,nickname,email,avatar from users where userId = #{id}")
    User searchById(Integer id);

    @Delete("delete from attention where `from` = #{from} and `to` = #{to}")
    Integer delAttention(@Param("from")Integer from,@Param("to")Integer to);
    //注册
    @Insert("INSERT INTO users(userId,account,password,nickname,email,avatar,isadmin) " +
            "VALUES(#{userId}, #{account}, #{password}, #{nickname}, #{email}, #{avatar},0)")
    void registerUser(User registerUser);
    // 添加关注记录
    @Insert("INSERT INTO attention(`from`, `to`) VALUES(#{from}, #{to})")
    Integer addAttention(@Param("from") Integer from, @Param("to") Integer to);
    // 检查用户名是否存在
    @Select("SELECT COUNT(*) FROM users WHERE account = #{username}")
    int isUsernameExists(@Param("username") String username);

    // 检查电子邮件是否存在
    @Select("SELECT COUNT(*) FROM users WHERE email = #{email}")
    int isEmailExists(@Param("email") String email);
    @Select("SELECT * FROM users WHERE userId = #{userId}")
    User isadmin( @Param("userId") Integer userId);
}
